#!/usr/bin/python2
# -*- coding: UTF-8 -*-

import logging
import urllib2
import urllib
import sys
import os
import re
import json
import codecs
import copy
import ConfigParser
import threading

from base import url_api

class TabelogService(threading.Thread):
    config_file = "./config/tabelog.json"
    thread_num = 0
    max_count = 0

    def __init__(self):
        threading.Thread.__init__(self)
        pass

    def __del__(self):
        pass

    def startup(self):
        if not self.__readConf(self.config_file):
            logging.error("read config %s failed" % self.config_file)
            return False

        self.start()

        return True

    def run(self):
        urls = self.config_dict['urls']
        for url in urls:
            goods_property = self.get_goods(url)
        pass

    def get_goods(self, url):
        goods_property = {}
        try:
            response = urllib2.urlopen(url)
            conent = response.read().decode('gbk')
        except Exception, error:
            logging.error(error)

        return goods_property

    def __readConf(self, config_path):
        if not os.path.exists(config_path):
            logging.error("don't exist config file " + config_path)
            return False

        config_file = open(config_path, "r")
        content = config_file.read()

        try:
            self.config_dict = json.loads(content)
        except Exception, e:
            logging.error("read config file:'%s' failed: %s" % (config_path, e))
            return False

        return True

def usage():
    print("Usage: python taobao_service.py [OPTION]... [ARGS]...")
    print("   -h, --help       print help")
    print("   -g, --goods      get goods information")
    print("   -s, --status     get running status")

if __name__ == '__main__':
    operator_list = ['help', 'goods', 'status']
    if len(sys.argv) < 3:
        usage()
        exit()

    if sys.argv[2] not in operator_list:
        usage()
        exit()

    urls = [
        "https://item.taobao.com/item.htm?id=548488131214"
    ]

    service = TabelogService()
    for url in urls:
        if not service.get_goods(url):
            pass
else:
    pass